﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SBACN.WebSite.Models;
using System.Linq.Expressions;
using System.Text;
using SBACN.WebSite.Code;

namespace SBACN.WebSite.MemberMsg
{
	public partial class MemberShow : MemberBasePage
	{
		protected string CTitle { get; set; }
		protected void Page_Load(object sender, EventArgs e)
		{
			CouncilInfoTitle();
			BindData();
			
		}

		protected void BindData()
		{
			Expression<Func<TB_SystemMap, bool>> f = (p => (p.Code.StartsWith("000100050") && p.Code.Length == 12));
			Expression<Func<TB_SystemMap, string>> order = (c => (c.Code));
			var resultM = DAL.DALHelper.Instance.GetList<TB_SystemMap, string>(f, order, true);

			if (resultM.Count > 0)
			{
				this.Member.DataSource = resultM;
				this.Member.DataBind();
			}

		}

		protected string MemberList(string Code)
		{
			// 是否有子分类
			Expression<Func<TB_SystemMap, bool>> f = (p => (p.Code.StartsWith(Code) && p.Code.Length == 16));
			Expression<Func<TB_SystemMap, string>> order = (c => (c.Code));
			var resultM = DAL.DALHelper.Instance.GetList<TB_SystemMap, string>(f, order, true);

			if (resultM.Count == 0)
			{
				// 查询单位
				return MemberMsg(Code);
			}
			else
			{
				StringBuilder CHtml = new StringBuilder();

				// 子分类
				foreach (var l in resultM)
				{
					string CName = "<div id='memdpbox'><div id='memdptitle'>" + l.CText + "</div><div  id='memdpBordertitle'>";
					CHtml.Append(CName);
					string Mmsg = MemberMsg(l.Code);
					if (!string.IsNullOrWhiteSpace(Mmsg))
						CHtml.Append(Mmsg);
					CHtml.Append("</div></div>");
				}
				return CHtml.ToString();
			}
		}

		protected string MemberMsg(string Code)
		{
			int Index = Convert.ToInt32(Request.QueryString["id"]);
			Expression<Func<View_CinfoRelationMember, bool>> f;
			if (string.IsNullOrWhiteSpace(Request.QueryString["id"]))
				f = (p => (p.Status == true && p.Type == Code));
			else
				f = (p => (p.Index == Index && p.Type == Code));

			Expression<Func<View_CinfoRelationMember, int>> order = (c => ((int)c.Memberorderby));
			var resultM = DAL.DALHelper.Instance.GetList<View_CinfoRelationMember, int>(f, order, true);

			StringBuilder MHtml = new StringBuilder();
			MHtml.Append("<ul>");
			if (resultM.Count > 0)
			{
				foreach (var l in resultM)
				{
					string Url = "javascript:void(0);";
					string Target = "_self";
					if (!string.IsNullOrWhiteSpace(l.WebSite))
					{
						Target = "_blank";
					if (!l.WebSite.Contains("http://"))
						Url = "http://" + l.WebSite;
					else 
						Url = l.WebSite;

					}

					string Msg = "<li><a target='" + Target + "' href='" + Url + "' rel='MemberMsg.aspx?id=" + l.MID + "' class='cluetip' title='" + l.MName + "' >" + l.MName + "</a></li>";
					MHtml.Append(Msg);

				}
			}
			MHtml.Append("</ul>");

			return MHtml.ToString();
		}

		protected void CouncilInfoTitle() {
			int Index = Convert.ToInt32(Request.QueryString["id"]);
			Expression<Func<TB_CouncilInfo, bool>> Tf;
			if (string.IsNullOrWhiteSpace(Request.QueryString["id"]))
				Tf = (tp => (tp.Status == true));
			else
				Tf = (tp => (tp.Index == Index));

			var resultCT = DAL.DALHelper.Instance.GetFirstOrDefaultObject<TB_CouncilInfo>(Tf);
			if (resultCT != null)
			{
				string StrTitle = resultCT.Name + "&nbsp;（";
				if (Convert.ToDateTime(resultCT.StartTime).ToString("yyyy-MM-dd") == "0001-01-01")
				{
					StrTitle = StrTitle + "";
				}
				else
				{
					StrTitle = StrTitle + resultCT.StartTime.ToString("yyyy.MM");
				}

				if (Convert.ToDateTime(resultCT.EndTime).ToString("yyyy-MM-dd") == "0001-01-01")
					StrTitle = StrTitle + " - 至今）";
				else
					StrTitle = StrTitle + " - " + Convert.ToDateTime(resultCT.EndTime).ToString("yyyy.MM") + "）";
				this.CTitle = StrTitle;
			}
		
		}

	}
}